﻿using System.Collections.Generic;

namespace Sidvall.AuthorizationManager.Model.DataPortalManagers
{
    public partial class AuthorizationScopeManager<TEntity, TList>
    {
        #region Public Members

        #region DeleteItemByPrimaryKeyAsync

        public async System.Threading.Tasks.Task<int> DeleteItemByPrimaryKeyAsync(int serverConnectionId, long applicationId)
        {
            global::Sidvall.Data.DeleteCriteria<global::Sidvall.AuthorizationManager.Data.Fields.AuthorizationScopeField> criteria;

            criteria = Sidvall.Data.CriteriaManager.NewDeleteCriteria<global::Sidvall.AuthorizationManager.Data.Fields.AuthorizationScopeField>();
            criteria.Filter = new global::Sidvall.Data.Filter<global::Sidvall.AuthorizationManager.Data.Fields.AuthorizationScopeField>();
            criteria.Filter.AddFilterItem(global::Sidvall.AuthorizationManager.Data.Fields.AuthorizationScopeField.AuthorizationScopeId, Sidvall.Data.ComparisonOperator.Equal, applicationId);
            ParameterManager.InitializeParameters(criteria, serverConnectionId);

            return await DeleteItemsAsync(criteria).ConfigureAwait(false);
        }

        #endregion
        #region GetItemByPrimaryKeyAsync

        public async System.Threading.Tasks.Task<TEntity> GetItemByPrimaryKeyAsync(int? serverConnectionId, long authorizationScopeId)
        {
            return await GetItemByPrimaryKeyAsync(serverConnectionId, authorizationScopeId, null).ConfigureAwait(false);
        }
        public async System.Threading.Tasks.Task<TEntity> GetItemByPrimaryKeyAsync(int? serverConnectionId, long authorizationScopeId, bool includeResourceActions, bool includeResourceItems, bool includeUserItems)
        {
            var includedEntities = new List<Data.Relations.AuthorizationScopeRelation>();
            if (includeResourceActions)
            {
                includedEntities.Add(Data.Relations.AuthorizationScopeRelation.AuthorizationScopeResourceActions);
                includedEntities.Add(Data.Relations.AuthorizationScopeRelation.AuthorizationScopeResourceActionsAuthorizationScopeResourceActionClaims);
            }
            if (includeResourceItems)
            {
                includedEntities.Add(Data.Relations.AuthorizationScopeRelation.AuthorizationScopeResourceItems);
                includedEntities.Add(Data.Relations.AuthorizationScopeRelation.AuthorizationScopeResourceItemsAuthorizationScopeResourceItemClaims);
            }
            if (includeUserItems)
            {
                includedEntities.Add(Data.Relations.AuthorizationScopeRelation.AuthorizationScopeUsers);
            }
            return await GetItemByPrimaryKeyAsync(serverConnectionId, authorizationScopeId, includedEntities).ConfigureAwait(false);
        }
        public async System.Threading.Tasks.Task<TEntity> GetItemByPrimaryKeyAsync(int? serverConnectionId, long authorizationScopeId, System.Collections.Generic.IEnumerable<global::Sidvall.AuthorizationManager.Data.Relations.AuthorizationScopeRelation> includedEntities)
        {
            global::Sidvall.Data.FetchCriteria<global::Sidvall.AuthorizationManager.Data.Fields.AuthorizationScopeField, global::Sidvall.AuthorizationManager.Data.Relations.AuthorizationScopeRelation> criteria;

            criteria = Sidvall.Data.CriteriaManager.NewFetchCriteria<global::Sidvall.AuthorizationManager.Data.Fields.AuthorizationScopeField, global::Sidvall.AuthorizationManager.Data.Relations.AuthorizationScopeRelation>(null, null, includedEntities, null, null);
            criteria.Filter = new global::Sidvall.Data.Filter<global::Sidvall.AuthorizationManager.Data.Fields.AuthorizationScopeField>();
            criteria.Filter.AddFilterItem(global::Sidvall.AuthorizationManager.Data.Fields.AuthorizationScopeField.AuthorizationScopeId, Sidvall.Data.ComparisonOperator.Equal, authorizationScopeId);
            ParameterManager.InitializeParameters(criteria, serverConnectionId);

            return await GetItemAsync(criteria).ConfigureAwait(false);
        }

        #endregion
        #region GetItemAllIncludedAsync

        public async System.Threading.Tasks.Task<TEntity> GetItemAllIncludedAsync(int? serverConnectionId, long authorizationScopeId, int? resourceItemRowCount, int? userRowCount)
        {
            TEntity item;
            List<global::Sidvall.AuthorizationManager.Data.Relations.AuthorizationScopeRelation> includedEntities;

            includedEntities = new List<Data.Relations.AuthorizationScopeRelation>();
            includedEntities.Add(Data.Relations.AuthorizationScopeRelation.AuthorizationScopeExports);
            includedEntities.Add(Data.Relations.AuthorizationScopeRelation.AuthorizationScopeResourceActions);
            includedEntities.Add(Data.Relations.AuthorizationScopeRelation.AuthorizationScopeResourceActionsAuthorizationScopeResourceActionClaims);
            item = await GetItemByPrimaryKeyAsync(serverConnectionId, authorizationScopeId, includedEntities).ConfigureAwait(false);
            if ((resourceItemRowCount == null) || (resourceItemRowCount > 0))
            {
                var authorizationScopeResourceItems = await this.DataPortalManager.GenericDataPortal.AuthorizationScopeResourceItemManager.GetItemsAllIncludedAsync(serverConnectionId, authorizationScopeId, resourceItemRowCount).ConfigureAwait(false);
                item.SetAuthorizationScopeResourceItems(authorizationScopeResourceItems);
            }
            if ((userRowCount == null) || (userRowCount > 0))
            {
                var users = await this.DataPortalManager.GenericDataPortal.AuthorizationScopeUserManager.GetUsersAsync(serverConnectionId, new long[] { authorizationScopeId },
                    null, userRowCount, false).ConfigureAwait(false);
                item.SetAuthorizationScopeUsers(users);
            }
            return item;
        }

        #endregion

        #endregion
    }
}